package com.liangtian.mdm.dm.jsp.common;

import com.liangtian.mdm.dm.common.constant.TableConst;
import com.liangtian.mdm.dm.common.constant.UrlConst;
import com.liangtian.mdm.dm.common.enums.Type;
import org.apache.commons.lang3.StringUtils;
import com.liangtian.mdm.dm.config.model.AttrConfig;
import com.liangtian.mdm.dm.model.model.Model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListJsCreate {

    /**
     * 查询页面js
     * 将拼接好的字符串内容替换掉自动生成的模板的内容
     */
    public static void createSearchJs(String readFilePath, Model model, String operType) {
        try {
            String script = replaceStr(readFilePath, model, operType, null); //每个列表页面的js都要替换的内容
            JspUtil.writeInJsp(script, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 变更历史页面js
     * 将拼接好的字符串内容替换掉自动生成的模板的内容
     */
    public static void createUpdateHisJs(String readFilePath, Model model, String operType) {
        try {
            String script = replaceUpdateHisStr(readFilePath, model, operType); //每个列表页面的js都要替换的内容
            JspUtil.writeInJsp(script, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 变更历史页面js
     * 将拼接好的字符串内容替换掉自动生成的模板的内容
     */
    public static void createBatchImportJs(String readFilePath, Model model, String operType) {
        try {
            String script = JspUtil.getTmpJspContent(readFilePath); //每个列表页面的js都要替换的内容
            script = script.replace("{PURL}", "/am/" + model.getTableName() + "/" + model.getTableName() + "_" + operType + ".jsp");
            script = script.replace("@R#", "\r\n"); //替换换行
            JspUtil.writeInJsp(script, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * <p>Description: 树模板查询页面js动态替换，调用的方法 </p>
     * <p>Company:@param readFilePath js路径
     * <p>Company:@param model 操作当前模型
     * <p>Company:@param operType --treeTabList  or treeTabZdList</p>
     *
     * @author : xiaoliu
     * @date :2016年12月14日上午11:22:10
     */
    public static void createTreeTabSearchJs(String readFilePath, Model model, String operType, List<AttrConfig> configLst) {
        try {
            String script = replaceTreeTabSearchStr(readFilePath, model, operType, configLst); //每个列表页面的js都要替换的内容
            JspUtil.writeInJsp(script, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 审核页面js
     * 将拼接好的字符串内容替换掉自动生成的模板的内容
     */
    public static void createShJs(String readFilePath, Model model, String operType, Map<String, Object> result_map, List<AttrConfig> configLst) {
        try {
            String script = replaceShStr(readFilePath, model, operType, result_map, configLst); //每个列表页面的js都要替换的内容
            JspUtil.writeInJsp(script, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 维护页面js
     * 将拼接好的字符串内容替换掉自动生成的模板的内容
     */
    public static void createWhJs(String readFilePath, Model model, String operType, List<AttrConfig> configLst) {
        try {
            String script = replaceWhStr(readFilePath, model, operType, configLst); //每个列表页面的js都要替换的内容
            JspUtil.writeInJsp(script, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 弹出列表页面js
     * 将拼接好的字符串内容替换掉自动生成的模板的内容
     */
    public static void createPopJs(String readFilePath, Model model, String columns, String columnName, AttrConfig confAttr, String operType) {
        try {
            String script = replacePopStr(readFilePath, model, columns, columnName, confAttr, operType); //每个列表页面的js都要替换的内容
            JspUtil.writeInJsp(script, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 弹出列表页面js
     * 将拼接好的字符串内容替换掉自动生成的模板的内容
     */
    public static void createNoTabPopJs(String readFilePath, Model model, String tabName, String columns, String columnName, String operType) {
        try {
            String script = replacePopNoTabStr(readFilePath, model, tabName, columns, columnName, operType); //每个列表页面的js都要替换的内容
            JspUtil.writeInJsp(script, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void createTreeJs(Model benModel, String readFilePath, Model model, String columns, AttrConfig conf, List<AttrConfig> treeConfigLst, String operType) {
        try {
            String script = replaceTreeStr(benModel, readFilePath, model, columns, conf, treeConfigLst, operType); //每个列表页面的js都要替换的内容
            JspUtil.writeInJsp(script, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void createBgPopJs(String readFilePath, Model model, String operType, String jspType) {
        String script = "";
        try {
            String tableName = model.getTableName();
            script = JspUtil.getTmpJspContent(readFilePath);
            script = script.replace("@R#", "\r\n");
            if ("bgPopList".equals(jspType)) {
                if ("0".equals(model.getClassType())) {//是列表
                    if (!StringUtils.isBlank(model.getMaintableId())) {//mainTablid不为空---说明是从表
                        script = script.replace("{tableUpdateUrl}", "'/am/" + tableName + "/" + tableName + "_" + operType + ".jsp?mainId='+$('#mainId').val()")
                                .replace("{subParamContent}", "pageReqeust.mainid=$('#mainId').val();");
                    } else {//说明是列表，且是主表
                        script = script.replace("{tableUpdateUrl}", "'/am/" + tableName + "/" + tableName + "_" + operType + ".jsp'")
                                .replace("{subParamContent}", "");
                    }
                } else if ("1".equals(model.getClassType())) {
                    script = script.replace("{tableUpdateUrl}", "'/am/" + tableName + "/" + tableName + "_treeTabUpdate.jsp?MainPid='+$('#bgPopPid').val()")
                            .replace("{subParamContent}", "");
                } else if ("2".equals(model.getClassType())) {
                    script = script.replace("{tableUpdateUrl}", "'/am/" + tableName + "/" + tableName + "_treeTabUpdate.jsp?MainColumn='+$('#bgPopColumn').val()")
                            .replace("{subParamContent}", "");
                }
            }
            if ("treeTabUpdate".equals(jspType)) {
                if ("0".equals(model.getClassType()) && !StringUtils.isBlank(model.getMaintableId())) {
                    script = script.replace("{tableUpdateUrl}", "'/am/" + tableName + "/" + tableName + "_" + jspType + ".jsp?mainId='+$('#mainId').val()")
                            .replace("{subParamContent}", "pageReqeust.mainid=$('#mainId').val();");
                } else {
                    script = script.replace("{tableUpdateUrl}", "'/am/" + tableName + "/" + tableName + "_" + jspType + ".jsp'")
                            .replace("{subParamContent}", "");
                }
            }
            script = script.replace("{modelId}", String.valueOf(model.getId()))
                    .replace("{tableId}", tableName + "_bgPop")
                    .replace("{tablepopName}", tableName)
                    .replace("{controlUrl}", UrlConst.LISTURL)
                    .replace("{toorBar}", tableName + "_bgToorBar")
                    .replace("{tableBgName}", tableName + "_" + operType)
                    .replace("{insertUrl}", UrlConst.INSERTURL);
            JspUtil.writeInJsp(script, readFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void createBgJs(String readFilePath, Model model, String operType, Map<String, Object> result_map, List<AttrConfig> jspAttrLst) {
        String script = replaceStr(readFilePath, model, operType, jspAttrLst);
        try {
            String tableName = model.getTableName();
            if ("update".equals(operType) || "treeTabUpdate".equals(operType)) {
                script = script.replace("{editUrl}", "/am/" + tableName + "/" + tableName + "_" + operType + "Edit.jsp");
            }
            Map<String, String> resMap = spellbgFun(model, jspAttrLst);
            if ("0".equals(model.getClassType()) && !StringUtils.isBlank(model.getMaintableId())) {//从表
                script = script.replace("{subParamContent}", " pageReqeust.mainid=$('#mainId').val();");
                script = script.replace("{delMain}", "");
                script = script.replace("{multipleAll}", resMap.get("multipleAllSub"));
                script = script.replace("{clickFun}", resMap.get("subClick"));
                script = script.replace("{updateSubmit}", "");
                script = script.replace("{subBgSpecial}", subBgFilter(true));
                script = script.replace("{appendMainid}", "+'&mainId='+$('#mainId').val()");
            } else {
                if ((boolean) result_map.get("flag")) {//主表
                    script = script.replace("{delMain}", ",'modelId':" + "'" + model.getId() + "'");
                } else {//没有主从关系的表
                    script = script.replace("{delMain}", "");
                }
                script = script.replace("{subParamContent}", "")
                        .replace("{appendMainid}", "")
                        .replace("{multipleAll}", resMap.get("multipleAllMain"))
                        .replace("{clickFun}", resMap.get("mainClick"))
                        .replace("{updateSubmit}", resMap.get("MainSubmit"))
                        .replace("{subBgSpecial}", subBgFilter(false));
            }
            script = script.replace("{styleOp}", operType).replace("{delControl}", UrlConst.DELETEURL);
            if ("update".equals(operType) || "apply".equals(operType) || "list".equals(operType) || "approve".equals(operType)) {
                script = script.replace("{viewDetailUrl}", "/am/" + tableName + "/" + tableName + "_detail.jsp?origin_id="); //替换查询变更历史路径
            } else {
                script = script.replace("{viewDetailUrl}", "/am/" + tableName + "/" + tableName + "_treeTabDetail.jsp?origin_id="); //替换查询变更历史路径
            }
            script = script.replace("{delControl}", UrlConst.DELETEURL)
                    .replace("@R#", "\r\n"); //替换换行

            JspUtil.writeInJsp(script, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static Map<String, String> spellbgFun(Model model, List<AttrConfig> jspAttrLst) {
        String tableName = model.getTableName();
        String bgTable = model.getTableName() + TableConst.MODIFYSUFFIX;
        Map<String, String> resMap = new HashMap<String, String>();
        StringBuilder sbMain = new StringBuilder();
        if ("0".equals(model.getClassType())) {//如果是列表
            sbMain.append("function multipleAll(str){ \r\n")
                    .append("$('#popcheckbox').modal({remote: basePath+'/am/" + tableName + "/" + tableName + "_bgPopList.jsp?operType='+str}); \r\n")
                    .append("} \r\n");
        } else {//如果是父子树
            sbMain.append("function multipleAll(str){ \r\n")
                    .append("$('#popcheckbox').modal({remote: basePath+'/am/" + tableName + "/" + tableName + "_bgPopList.jsp?operType='+str+'&" + JspUtil.spellColumn(model, Type.UPDATE) + "='+$('#" + JspUtil.spellColumn(model, Type.UPDATE) + "').val()}); \r\n")
                    .append("} \r\n");
        }
        resMap.put("multipleAllMain", sbMain.toString());
        //从表【冻结、解冻】
        StringBuilder sbSub = new StringBuilder();
        sbSub.append("    function multipleAll(str){ $RN$")
                .append("          var data = $('#" + bgTable + "').bootstrapTable('getSelections'); $RN$")
                .append("          var status, stName; $RN$")
                .append("           if(str=='active'){ $RN$")
                .append("	            status = '0'; $RN$")
                .append("	            stName = '活动'; $RN$")
                .append("           }else if(str=='freeze'){ $RN$")
                .append("	            status = '1'; $RN$")
                .append("	            stName = '冻结'; $RN$")
                .append("           } $RN$")
                .append("       var idArr = []; $RN$")
                .append("       for(var i=0;i<data.length;i++){  $RN$")
                .append("	        if(data[i].opertype!=stName){ $RN$")
                .append("		      idArr.push(data[i]); $RN$")
                .append("	        } $RN$")
                .append("        } $RN$")
                .append("     var uId=filterArray(idArr); $RN$")
                .append("        if(uId.length>0){ $RN$")
                .append("       	$.ajax({  $RN$")
                .append("		       type: 'POST',  $RN$")
//			.append("		       url: basePath+'/jspAction/changeStatus',  $RN$") 
                .append("		       url: basePath+'" + UrlConst.CHANGESTATUSURL + "',  $RN$")
                .append("		       data: {'ids':uId.join(','), 'status':status, 'tableName':'" + bgTable + "', opertype:str}, $RN$")
                .append("		       success: function(data){  $RN$")
                .append("		        	if(data.success){  $RN$")
                .append("				       toastr.success('操作成功!');  $RN$")
                .append("				       $('#" + bgTable + "').bootstrapTable('refresh');  $RN$")
                .append("			        }else{  $RN$")
                .append("				       toastr.warning('操作失败!');  $RN$")
                .append("				       $('#" + bgTable + "').bootstrapTable('refresh');  $RN$")
                .append("			        }  $RN$")
                .append("		      }  $RN$")
                .append("	       });  $RN$")
                .append("        }else{  $RN$")
                .append("	         toastr.warning('请选择需要操作数据!');  $RN$")
                .append("	         return;  $RN$")
                .append("        } $RN$")
                .append("      } $RN$");
        resMap.put("multipleAllSub", sbSub.toString().replace("$RN$", "\r\n"));
        //提交
        StringBuilder sbMainSubmit = new StringBuilder();
        sbMainSubmit.append("function submit(){ $RN$")
                .append("var data = $('#" + bgTable + "').bootstrapTable('getSelections'); $RN$")
                .append("if(data.length>=1){ $RN$")
                .append("	submitByIds(data); $RN$")
                .append("}else{ $RN$")
                .append("	toastr.warning('请选择需要操作数据!'); $RN$")
                .append("	return; $RN$ } $RN$ } $RN$")
                .append("function submitByIds(data){ $RN$")
                .append("   var list =new Array(); $RN$")
                .append("   for(var i=0;i<data.length;i++){ $RN$")
                .append("      var obj = { $RN$")
                .append("        dataId : data[i].id, $RN$")
                .append(AddJsCreate.spellWeiYiMethod(jspAttrLst))
                .append("        operType : data[i].opertype, $RN$")
                .append("        tableName : '" + bgTable + "', $RN$")
                .append("        dataType : '' $RN$")
                .append("      }; $RN$")
                .append("      list.push(obj); $RN$")
                .append("   } $RN$")
                .append("$('#loading').toggle(); $RN$")
                .append("$.ajax({ $RN$")
                .append("	type: 'POST', $RN$")
                .append("	url: basePath+'" + UrlConst.SUBMITURL + "', $RN$")
                .append("   dataType:'json', $RN$")
                .append("   contentType:'application/json', $RN$")
                .append(" 	data: JSON.stringify(list), $RN$")
                .append("	success: function(data){ $RN$")
                .append("if(data.success){ $RN$")
//			.append("	toastr.success('提交成功!'); $RN$")
                .append("	toastr.success(data.msg); $RN$") // 自定义异常
                .append("   $('#loading').toggle(); $RN$")
                .append("	$('#" + bgTable + "').bootstrapTable('refresh'); $RN$")
                .append("}else{ $RN$")
//			.append("	toastr.warning('提交失败!'); $RN$")
                .append("	toastr.warning(data.msg);  $RN$")
                .append("   $('#loading').toggle(); $RN$")
                .append("	$('#" + bgTable + "').bootstrapTable('refresh'); $RN$")
                .append("} $RN$ } $RN$ }); $RN$")
                .append("} $RN$ ");
        resMap.put("MainSubmit", sbMainSubmit.toString().replace("$RN$", "\r\n"));

        StringBuilder mainClick = new StringBuilder();
        mainClick.append("     $('.glyphicon-plus').parent().click(function(){ $RN$")
                .append("	           multipleAll('update'); $RN$")
                .append("          }); $RN$")
                .append("          $('.glyphicon-arrow-up').parent().click(function(){ $RN$")
                .append("              submit(); $RN$")
                .append("          }); $RN$");
        //主表【冻结、解冻】
        StringBuilder sb = new StringBuilder();
        sb.append("          $('.glyphicon-ok').parent().click(function(){ $RN$")
                .append("	         multipleAll('active'); $RN$")
                .append("         }); $RN$")
                .append("        $('.glyphicon-remove').parent().click(function(){ $RN$")
                .append("          	 multipleAll('freeze'); $RN$")
                .append("         }); $RN$")
                .append("        $('.glyphicon-edit').parent().click(function(){ $RN$")
                .append(" 	          alter(); $RN$")
                .append("         }); $RN$")
                .append("         $('.glyphicon-trash').parent().click(function(){ $RN$")
                .append("	          del(); $RN$")
                .append("         }); $RN$");
        resMap.put("mainClick", mainClick.append(sb).toString().replace("$RN$", "\r\n"));

        //子表【变更新增】
        StringBuilder subClick = new StringBuilder();
        subClick.append("    $('.glyphicon-plus').parent().click(function(){ $RN$")
                .append("                location.href=basePath+'/am/" + tableName + "/" + tableName + "_updateAdd.jsp?operType=updateAdd&mainId='+$('#mainId').val(); $RN$")
                .append("      }); $RN$");
        resMap.put("subClick", subClick.append(sb).toString().replace("$RN$", "\r\n"));
        return resMap;
    }

    /**
     * 变更的从表数据【禁止删除之前的明细】
     *
     * @add 2016-11-14 zhangtao
     */
    public static String subBgFilter(boolean isNeed) {
        if (isNeed) {
            StringBuilder filter = new StringBuilder();
            filter.append("     for(var i=0;i<data.length;i++){ \r\n")
                    .append("       	if(data[i].opertype=='新增') \r\n")
                    .append(" 		filterData.push(data[i]); \r\n")
                    .append("     } \r\n")
                    .append("     if(data.length>0 && filterData.length==0){ \r\n")
                    .append("	     	toastr.warning('只允许删除本次变更的新增数据！'); \r\n")
                    .append("		    return;	\r\n")
                    .append("     } \r\n");
            return filter.toString();
        } else {
            return "filterData = data;  \r\n";
        }
    }

    /**
     * 级联设置弹出列表页面js
     * 将拼接好的字符串内容替换掉自动生成的模板的内容
     */
    public static void createPopCaseJs(String readFilePath, Model model, String columns, String columnName, String caseCondition, AttrConfig confAttr, String operType) {
        try {
            String script = replacePopCaseStr(readFilePath, model, columns, columnName, caseCondition, confAttr, operType); //每个列表页面的js都要替换的内容
            JspUtil.writeInJsp(script, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 申请页面js
     * 将拼接好的字符串内容替换掉自动生成的模板的内容
     */
    public static void createApplyJs(String readFilePath, Model model, String operType, boolean bool, List<AttrConfig> jspAttrLst) {
        try {
            String tableName = model.getTableName();
            String script = replaceStr(readFilePath, model, operType, jspAttrLst); //每个列表页面的js都要替换的内容
            if ("0".equals(model.getClassType()) && !StringUtils.isBlank(model.getMaintableId())) {
                script = script.replace("{delMain}", "")
                        .replace("{addUrl}", "'/am/" + tableName + "/" + tableName + "_add.jsp?mainId='+$('#mainId').val()")
                        .replace("{subParamContent}", " pageReqeust.mainid=$('#mainId').val();")
                        .replace("{appendMainid}", "+'&&mainId='+$('#mainId').val()")
                        .replace("{checkMainTableData}", spellCheckMainDataIsSave());
            } else {
                if (bool)
                    script = script.replace("{delMain}", ",'modelId':" + "'" + model.getId() + "'");
                else
                    script = script.replace("{delMain}", "");
                script = script.replace("{addUrl}", "'/am/" + tableName + "/" + tableName + "_add.jsp'") //替换新增按钮路径/dm/model/modelAdd.jsp';
                        .replace("{subParamContent}", "")
                        .replace("{appendMainid}", "")
                        .replace("{checkMainTableData}", "");
            }
            script = script.replace("{viewDetailUrl}", "/am/" + tableName + "/" + tableName + "_detail.jsp?origin_id=") //替换查询变更历史路径
                    .replace("{editUrl}", "/am/" + tableName + "/" + tableName + "_edit.jsp") //替换编辑按钮路径/dm/model/modelEdit.jsp';
                    .replace("{delControl}", UrlConst.DELETEURL) //替换返回按钮路径返回列表页面
                    .replace("{submitControl}", UrlConst.SUBMITURL) //替换返回按钮路径返回列表页面
                    .replace("{checkControl}", UrlConst.CHECKURL); //替换返回按钮路径返回列表页面
            JspUtil.writeInJsp(script, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String spellCheckMainDataIsSave() {
        StringBuilder sb = new StringBuilder();
        sb.append("     if('null' == $('#mainId').val()){ \r\n")
                .append("        toastr.warning('请先填写主表数据!'); \r\n")
                .append("        return; \r\n")
                .append("     }else \r\n");
        return sb.toString();
    }

    /**
     * <p>Description: 生成树模板的js页面 </p>
     * <p>@param readFilePath treeTabMain 的js路径
     * <p>@param model  当前操作模型
     * <p>@param operType --treeTabMain
     * <p>@param bool </p>
     *
     * @author : xiaoliu
     * @date :2016年12月14日上午10:01:37
     */
    public static void createTreeTabMainJs(String readFilePath, Model model, Model fenModel, String operType,
                                           boolean bool, List<AttrConfig> concigLst) {
        try {
            //String tableName = model.getTableName();
            String script = JspUtil.getTmpJspContent(readFilePath); //每个列表页面的js都要替换的内容
            script = script.replace("{treeJsReplace}", spellTreeJsReplace(model, fenModel, operType, concigLst));
            script = script.replace("@R#", "\r\n"); //替换换行
            //		   	if("2".equals(model.getClassType())){//分类树
//					if(model.getMaintableId()!=null){
//						script=script.replace("{tableName}", TableConst.TABLEPREFIX+model.getMaintableId());
//						script=script.replace("{modelName}",fenModel.getModelName());
//						script=script.replace("{tableTree}", TableConst.TABLEPREFIX+model.getMaintableId()+"Tree");
//					}else{
//						script=script.replace("{tableName}", tableName).replace("{tableTree}", tableName+"Tree");
//					}
//				}else{
//					script=script.replace("{tableName}", tableName);
//					script=script.replace("{modelName}",model.getModelName());
//					script=script.replace("{tableTree}", tableName+"Tree");
//				}
//		           script = script.replace("@R#", "\r\n") //替换换行
//		            .replace("{treeSearchURL}", UrlConst.TREENODESURL) //查询的树节点的地址
//			        .replace("{tableFrame}", tableName+"Frame")
//			        .replace("{MainColumn}", JspUtil.spellColumn(model,Type.MAIN));
            JspUtil.writeInJsp(script, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void createTreeTabApplyJs(String readFilePath, Model model, String operType, boolean bool, List<AttrConfig> jspAttrLst) {
        try {
            String script = replaceStr(readFilePath, model, operType, jspAttrLst); //每个列表页面的js都要替换的内容
            String tableName = model.getTableName();
            if ("0".equals(model.getClassType()) && !StringUtils.isBlank(model.getMaintableId())) {
                script = script.replace("{delMain}", "")
                        .replace("{addUrl}", "'/am/" + tableName + "/" + tableName + "_treeTabAdd.jsp?mainId='+$('#mainId').val()")
                        .replace("{subParamContent}", " pageReqeust.mainid=$('#mainId').val();")
                        .replace("{appendMainid}", "+'&mainId='+$('#mainId').val()")
                        .replace("{checkMainTableData}", spellCheckMainDataIsSave());
            } else {
                if (bool)
                    script = script.replace("{delMain}", ",'modelId':" + "'" + model.getId() + "'");
                else
                    script = script.replace("{delMain}", "");
                script = script.replace("{addUrl}", "'/am/" + tableName + "/" + tableName + "_treeTabAdd.jsp?" + JspUtil.spellColumn(model, Type.APPLY) + "='+$('#" + JspUtil.spellColumn(model, Type.APPLY) + "').val()") //替换新增按钮路径/dm/model/modelAdd.jsp';
                        .replace("{subParamContent}", "")
                        .replace("{appendMainid}", "")
                        .replace("{checkMainTableData}", "");
            }
            script = script.replace("{styleOp}", operType) //表的id
                    .replace("{tableId}", tableName); //表的id

            script = script.replace("{tableSearchURL}", UrlConst.LISTURL) //查询的controller地址
                    .replace("{editUrl}", "/am/" + tableName + "/" + tableName + "_treeTabEdit.jsp") //替换新增按钮路径/dm/model/modelAdd.jsp';) //替换编辑按钮路径/dm/model/modelEdit.jsp';
                    .replace("{delControl}", UrlConst.DELETEURL) //替换返回按钮路径返回列表页面
                    .replace("{submitControl}", UrlConst.SUBMITURL) //替换返回按钮路径返回列表页面
                    .replace("{checkControl}", UrlConst.CHECKURL)//替换返回按钮路径返回列表页面
                    .replace("{fenColumnReplace}", spellfenColumn(model, operType, jspAttrLst))
                    .replace("{classColumn}", JspUtil.spellColumn(model, Type.TREE))
                    .replace("{viewColumn}", spellViewColumn(model, operType, jspAttrLst))
                    .replace("{MainColumn}", JspUtil.spellColumn(model, Type.MAIN))
                    .replace("{ApplyColumn}", JspUtil.spellColumn(model, Type.APPLY))
                    .replace("@R#", "\r\n"); //替换换行;

            JspUtil.writeInJsp(script, readFilePath); //将替换后的、正式的字符串重新写入生成的jsp里
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 每个列表页面的js都要替换的内容
     */
    public static String replaceStr(String readFilePath, Model model, String operType, List<AttrConfig> jspAttrLst) {
        String script = "";
        try {
            String tableName = model.getTableName();
            script = JspUtil.getTmpJspContent(readFilePath);  //根据路径获取自动生成的模板内

            if ("update".equals(operType) || "treeTabUpdate".equals(operType)) {
                script = script.replace("{tableId}", tableName + TableConst.MODIFYSUFFIX); //表的id
            } else {
                script = script.replace("{tableId}", tableName) //表的id
                        .replace("{surveyType}", tableName) //替换业务类型
                        .replace("{viewUpdateHisUrl}", "/am/" + tableName + "/" + tableName + "_updateHis.jsp?origin_id="); //替换查询变更历史路径
            }
            if ("apply".equals(operType) || "treeTabApply".equals(operType) || "applyDetail".equals(operType) || "treeTabApplyDetail".equals(operType)) {
                script = script.replace("{operType}", "apply"); //替换操作类型
                script = script.replace("{subCheckParams}", AddJsCreate.spellWeiYiMethod(jspAttrLst));
            } else if ("update".equals(operType) || "treeTabUpdate".equals(operType)) {
                script = script.replace("{operType}", "update"); //替换操作类型
                script = script.replace("{fenColumnReplace}", spellfenColumn(model, operType, jspAttrLst));
                script = script.replace("{classColumn}", JspUtil.spellColumn(model, Type.TREE));
                script = script.replace("{MainColumn}", JspUtil.spellColumn(model, Type.MAIN));
                script = script.replace("{UpdateColumn}", JspUtil.spellColumn(model, Type.UPDATE));
            } else {
                script = script.replace("{operType}", operType); //替换操作类型
            }
            script = script.replace("{styleOp}", operType); //判断是从哪个页面点击过来的
            if ("update".equals(operType) || "apply".equals(operType) || "list".equals(operType) || "approve".equals(operType)) {
                script = script.replace("{viewDetailUrl}", "/am/" + tableName + "/" + tableName + "_detail.jsp?origin_id="); //替换查询变更历史路径

            } else {
                script = script.replace("{viewDetailUrl}", "/am/" + tableName + "/" + tableName + "_treeTabDetail.jsp?origin_id="); //替换查询变更历史路径
            }
            script = script.replace("{controlUrl}", UrlConst.LISTURL) //查询的controller地址
                    .replace("@R#", "\r\n") //替换换行
                    .replace("{dispatchControl}", UrlConst.INSERTURL) //替换插入分发表
                    .replace("{modelId}", String.valueOf(model.getId())); //替换modelid
        } catch (Exception e) {
            e.printStackTrace();
        }
        return script;
    }

    /**
     * 每个列表页面的js都要替换的内容
     */
    public static String replaceUpdateHisStr(String readFilePath, Model model, String operType) {
        String script = "";
        try {
            String tableName = model.getTableName();
            script = JspUtil.getTmpJspContent(readFilePath)  //根据路径获取自动生成的模板内
                    .replace("{surveyType}", tableName + TableConst.MODIFYSUFFIX) //替换业务类型
                    .replace("{controlUrl}", UrlConst.LISTURL) //查询的controller地址
                    .replace("@R#", "\r\n") //替换换行
                    .replace("{modelId}", String.valueOf(model.getId())) //替换modelid
                    .replace("{operType}", "update"); //替换操作类型
            if ("treeTabUpdateHis".equals(operType)) {
                script = script.replace("{backUrl}", "/am/" + tableName + "/" + tableName + "_treeTabList.jsp") //替换操作类型
                        .replace("{tableId}", tableName + "_updateHis")
                        .replace("{UpdateHisColumn}", JspUtil.spellColumn(model, Type.UPDATEHIS))
                        .replace("{ListColumn}", JspUtil.spellColumn(model, Type.QUERY))
                        .replace("{MainColumn}", JspUtil.spellColumn(model, Type.MAIN)); //表的id
            } else if ("updateHis".equals(operType)) {
                script = script.replace("{backUrl}", "/am/" + tableName + "/" + tableName + "_list.jsp") //替换操作类型
                        .replace("{tableId}", tableName + "_" + operType); //表的id
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return script;
    }

    /**
     * 每个列表页面的js都要替换的内容
     */
    public static String replaceShStr(String readFilePath, Model model, String operType, Map<String, Object> result_map, List<AttrConfig> configLst) {
        String script = "";
        try {
            script = JspUtil.getTmpJspContent(readFilePath)  //根据路径获取自动生成的模板内容
                    .replace("{tableId}", model.getTableName()) //表的id
                    .replace("{controlUrl}", UrlConst.LISTURL) //查询的controller地址
                    .replace("{controlApproveUrl}", UrlConst.APPROVEURL) //审核的controller地址
                    .replace("@R#", "\r\n") //替换换行
                    .replace("{modelId}", String.valueOf(model.getId())) //替换modelid
                    .replace("{surveyType}", model.getTableName()); //替换业务类型
            if ("treeTabApprove".equals(operType)) {//树模板的审核
                script = script.replace("{viewDetailUrl}", "/am/" + model.getTableName() + "/" + model.getTableName() + "_treeTabDetail.jsp?origin_id=");//替换查询变更历史路径
            } else {
                script = script.replace("{viewDetailUrl}", "/am/" + model.getTableName() + "/" + model.getTableName() + "_detail.jsp?origin_id="); //替换查询变更历史路径
            }
            script = script.replace("{operType}", "approve")
                    .replace("{styleOp}", operType)
                    .replace("{fenColumnReplace}", spellfenColumn(model, operType, configLst))
                    .replace("{classColumn}", JspUtil.spellColumn(model, Type.TREE))
                    .replace("{ShColumn}", JspUtil.spellColumn(model, Type.APPROVE))
                    .replace("{viewColumn}", ListJsCreate.spellViewColumn(model, operType, configLst))
                    .replace("{MainColumn}", JspUtil.spellColumn(model, Type.MAIN))
                    .replace("{subTable}", model.getTableName() + TableConst.MODIFYSUFFIX); //替换操作类型
        } catch (Exception e) {
            e.printStackTrace();
        }
        return script;
    }

    /**
     * <p>Description:树模板查询js动态替换 </p>
     * <p>Company:@param readFilePath js路径
     * <p>Company:@param model -模型
     * <p>Company:@param operType  --treeTabList or treeTabzdList
     * <p>Company:@return </p>
     *
     * @author : xiaoliu
     * @date :2016年12月14日上午11:20:45
     */
    public static String replaceTreeTabSearchStr(String readFilePath, Model model, String operType, List<AttrConfig> configLst) {
        String script = "";
        try {
            String tableName = model.getTableName();
            script = JspUtil.getTmpJspContent(readFilePath)  //根据路径获取自动生成的模板内容
                    .replace("{tableId}", tableName); //表的id
            script = script.replace("{tableSearchURL}", UrlConst.LISTURL) //查询的controller地址
                    .replace("@R#", "\r\n") //替换换行
                    .replace("{modelId}", String.valueOf(model.getId())) //替换modelid
                    .replace("{surveyType}", tableName) //替换业务类型
                    .replace("{operType}", "list") //替换操作类型
                    .replace("{styleOp}", operType) //替换查询变更历史路径
                    .replace("{dispatchControl}", UrlConst.INSERTURL)//替换插入分发表
                    .replace("{classColumn}", JspUtil.spellColumn(model, Type.TREE))
                    //.replace("{fenColumnReplace}", spellfenColumn(model,configLst))
                    .replace("{MainColumn}", JspUtil.spellColumn(model, Type.MAIN))
                    .replace("{ListColumn}", JspUtil.spellColumn(model, Type.QUERY));
            script = script.replace("{fenColumnReplace}", spellfenColumn(model, operType, configLst));

            if ("treeTabList".equals(operType) || "treeTabzdList".equals(operType)) {
                script = script.replace("{viewUpdateHisUrl}", "/am/" + tableName + "/" + tableName + "_treeTabUpdateHis.jsp?origin_id="); //替换查询变更历史路径
            }
            if ("list".equals(operType)) {
                script = script.replace("{viewDetailUrl}", "/am/" + tableName + "/" + tableName + "_detail.jsp?origin_id="); //替换查询变更历史路径
            } else {
                script = script.replace("{viewDetailUrl}", "/am/" + tableName + "/" + tableName + "_treeTabDetail.jsp?origin_id="); //替换查询变更历史路径
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return script;
    }

    public static String replaceTreeTabStr(String readFilePath, Model model, String operType) {
        String script = "";
        try {
            String tableName = model.getTableName();
            script = JspUtil.getTmpJspContent(readFilePath)  //根据路径获取自动生成的模板内容
                    .replace("{tableId}", tableName); //表的id
            script = script.replace("{tableSearchURL}", UrlConst.LISTURL) //查询的controller地址
                    .replace("@R#", "\r\n") //替换换行
                    .replace("{modelId}", String.valueOf(model.getId())) //替换modelid
                    .replace("{surveyType}", tableName) //替换业务类型
                    .replace("{operType}", "list") //替换操作类型
                    .replace("{styleOp}", operType) //替换查询变更历史路径
                    .replace("{dispatchControl}", UrlConst.INSERTURL)//替换插入分发表
                    .replace("{classColumn}", JspUtil.spellColumn(model, Type.TREE))
                    .replace("{ListColumn}", JspUtil.spellColumn(model, Type.QUERY));

            if ("treeTabList".equals(operType) || "treeTabzdList".equals(operType)) {
                script = script.replace("{viewUpdateHisUrl}", "/am/" + tableName + "/" + tableName + "_treeTabUpdateHis.jsp?origin_id="); //替换查询变更历史路径
            }
            script = script.replace("{styleOp}", operType); //判断是从哪个页面点击过来的
            if ("update".equals(operType) || "apply".equals(operType) || "list".equals(operType) || "approve".equals(operType)) {
                script = script.replace("{viewDetailUrl}", "/am/" + tableName + "/" + tableName + "_detail.jsp?origin_id="); //替换查询变更历史路径
            } else {
                script = script.replace("{viewDetailUrl}", "/am/" + tableName + "/" + tableName + "_treeTabDetail.jsp?origin_id="); //替换查询变更历史路径
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return script;
    }

    /**
     * 每个列表页面的js都要替换的内容
     *
     * @param readFilePath
     * @param model
     * @param operType
     * @return
     */
    public static String replaceWhStr(String readFilePath, Model model, String operType, List<AttrConfig> configLst) {
        String script = "";
        try {
            String tableName = model.getTableName();
            script = JspUtil.getTmpJspContent(readFilePath)  //根据路径获取自动生成的模板内容
                    .replace("{tableId}", tableName) //表的id
                    .replace("{controlUrl}", UrlConst.LISTURL) //查询的controller地址
                    .replace("{treeSearchURL}", UrlConst.TREENODESURL) //查询的controller地址
                    .replace("{tableSearchURL}", UrlConst.LISTURL) //查询的controller地址
                    .replace("@R#", "\r\n") //替换换行
                    .replace("{modelId}", String.valueOf(model.getId())) //替换modelid
                    .replace("{surveyType}", tableName)
                    .replace("{fenColumnReplace}", spellfenColumn(model, operType, configLst))
                    .replace("{classColumn}", JspUtil.spellColumn(model, Type.TREE))
                    .replace("{WhColumn}", JspUtil.spellColumn(model, Type.WH)); //替换业务类型
            script = script.replace("{operType}", "list"); //替换操作类型
            if ("wh".equals(operType)) {
                script = script.replace("{viewDetailUrl}", "/am/" + tableName + "/" + tableName + "_detail.jsp?origin_id=");
            } else if ("treeTabWh".equals(operType)) {
                script = script.replace("{viewDetailUrl}", "/am/" + tableName + "/" + tableName + "treeTabDetail.jsp?origin_id=");
            }
            script = script.replace("{updateStatusControl}", UrlConst.CHANGESTATUSURL) //修改路径
                    .replace("{editUrl}", "/am/" + tableName + "/" + tableName + "_edit.jsp") //替换编辑按钮路径/dm/model/modelEdit.jsp';
                    .replace("{editTreeUrl}", "/am/" + tableName + "/" + tableName + "_treeTabEdit.jsp"); //替换编辑按钮路径/dm/model/modelEdit.jsp';
        } catch (Exception e) {
            e.printStackTrace();
        }
        return script;
    }

    /**
     * 每个列表页面的js都要替换的内容
     *
     * @param readFilePath
     * @param model
     * @param operType
     * @return {"c_001":"中国"，"c_002":"北京"}拼写级联类型的参数
     */
    public static String replacePopStr(String readFilePath, Model model, String columns, String columnName, AttrConfig confAttr, String operType) {
        String script = "";
        try {
            script = JspUtil.getTmpJspContent(readFilePath)  //根据路径获取自动生成的模板内容
                    .replace("{tableId}", model.getTableName() + "_" + columnName + "_pop") //表的id
                    .replace("{popColId}", columnName.toLowerCase()) //选择列的id
                    .replace("{popColumn}", columnName.toLowerCase() + "Name") //选择列的名
                    .replace("{popColumns}", columns)
                    .replace("{toorBar}", model.getTableName() + "_" + columnName + "_toorBar")
                    .replace("{controlUrl}", UrlConst.POPLISTURL) //查询的controller地址
                    .replace("pageReqeust.filter={{cascad}};", "")
                    .replace("@R#", "\r\n") //替换换行
                    .replace("{modelId}", String.valueOf(model.getId())) //替换modelid
                    .replace("{operColumn}", columnName)
                    .replace("{surveyType}", model.getTableName()) //替换业务类型
                    .replace("{tableName}", model.getTableName())
                    .replace("{queryParams}", "queryParamsTab")
                    .replace("{idFieldRep}", "idField: \"id\",")
                    .replace("{popColRep}", "$(\"#" + columnName.toLowerCase() + "\").val(data[0].id);$(\"#" + columnName.toLowerCase() + "Name" + "\").val(" + columns + ");")
                    .replace("{clearPopJiLianData}", clearPopJiLianData(confAttr))// confAttr  clearPopJiLianData(confAttr)
                    .replace("{operType}", operType); //替换操作类型
        } catch (Exception e) {
            e.printStackTrace();
        }
        return script;
    }

    public static String replacePopNoTabStr(String readFilePath, Model model, String tabName, String columns, String columnName, String operType) {
        String script = "";
        try {
            script = JspUtil.getTmpJspContent(readFilePath)  //根据路径获取自动生成的模板内容
                    .replace("{tableId}", tabName + "_" + columnName + "_pop") //表的id
                    .replace("{popColId}", columnName.toLowerCase()) //选择列的id
                    .replace("{popColumn}", columnName.toLowerCase() + "Name") //选择列的名
                    .replace("{popColumns}", columns)
                    .replace("{toorBar}", model.getTableName() + "_" + columnName + "_toorBar")
                    .replace("{controlUrl}", "/jspAction/queryNoTabList") //查询的controller地址
                    .replace("pageReqeust.filter={{cascad}};", "")
                    .replace("@R#", "\r\n") //替换换行
                    .replace("{modelId}", String.valueOf(model.getId())) //替换modelid
                    .replace("{surveyType}", model.getTableName()) //替换业务类型
                    .replace("{modelId}", String.valueOf(model.getId())) //替换modelid
                    .replace("{operColumn}", columnName)
                    .replace("{tableName}", tabName)
                    .replace("{queryParams}", "queryParamsNoTab")
                    .replace("{idFieldRep}", "")
                    .replace("{popColRep}", "$(\"#" + columnName.toLowerCase() + "\").val(" + columns + ");")
                    .replace("{clearPopJiLianData}", "")
                    .replace("{operType}", operType); //替换操作类型
        } catch (Exception e) {
            e.printStackTrace();
        }
        return script;
    }

    public static String replaceTreeStr(Model benModel, String readFilePath, Model model,
                                        String columns, AttrConfig conf, List<AttrConfig> treeConfigLst, String operType) {
        String script = "";
        try {
            String columnLowerCase = conf.getAttrColumn().toLowerCase();
            script = JspUtil.getTmpJspContent(readFilePath)  //根据路径获取自动生成的模板内容
                    .replace("{treeId}", model.getTableName() + conf.getAttrColumn() + "tree") //表的id
                    .replace("{treeColumn}", columnLowerCase + "Name") //选择列的名
                    .replace("{tableName}", model.getTableName())
                    .replace("{modelName}", model.getModelName());
            if (benModel.getClassType().equals("2")) {//如果本模型是分类树
//				   script= script.replace("{viewName}", conf.getViewName())
//				      .replace("{viewColumn}", conf.getAttrColumn());
                if (model.getClassType().equals("1")) {//如果引用的树是父子树
                    //script= script.replace("{viewName}", conf.getViewName())
//							      .replace("{viewColumn}", conf.getTreeViewColumn());
                    script = script.replace("{viewName}", model.getModelName())
                            .replace("{attrColumnTree}", conf.getTreeViewColumn()) //获取原模型列名
//									   .replace("{viewColumn}", benModel.getClassColumn()); 
                            .replace("{attrColumn}", conf.getTreeViewColumn())
                            .replace("{treeSearchNodeURL}", UrlConst.TREENODESURL) //查询的controller地址
                            .replace("{viewColumn}", conf.getTreeViewColumn());
                } else {
//					   script= script.replace("{viewName}", benModel.getTableName())
                    script = script.replace("{viewName}", benModel.getModelName())
                            .replace("{attrColumnTree}", conf.getAttrColumn()) //获取原模型列名
                            .replace("{attrColumn}", conf.getAttrColumn())
                            .replace("{treeSearchNodeURL}", UrlConst.TREESEARCHNODE) //查询的controller地址
                            .replace("{viewColumn}", benModel.getClassColumn());
                }

            } else if (benModel.getClassType().equals("1")) {//如果本模型是父子树
//				   if(benModel.equals(model)){//如果父子树引用的是本
                if (conf.getIsParentType() != 1) {
                    if (conf.getTreeViewColumn() == null) {
                        List<AttrConfig> isPLst = new ArrayList<AttrConfig>();
                        // String column = "";
                        if (treeConfigLst.size() > 0) {
                            for (int i = 0; i < treeConfigLst.size(); i++) {
                                AttrConfig spleeCon = treeConfigLst.get(i);
                                if (1 == spleeCon.getIsParentType()) {//如果是父子树上下级关系
                                    isPLst.add(spleeCon);
                                }
                            }
                        }
                        if (isPLst.size() > 0) {
                            script = script.replace("{viewName}", model.getModelName())
                                    .replace("{attrColumnTree}", isPLst.get(0).getAttrColumn()) //获取原模型列名
                                    .replace("{attrColumn}", conf.getAttrColumn())
                                    .replace("{treeSearchNodeURL}", UrlConst.TREESEARCHNODE) //查询的controller地址
                                    .replace("{viewColumn}", isPLst.get(0).getAttrColumn());
                        }
                    }
                } else {
                    script = script.replace("{viewName}", conf.getViewName())
                            .replace("{attrColumnTree}", conf.getAttrColumn()) //获取原模型列名
                            .replace("{attrColumn}", conf.getAttrColumn())
                            .replace("{treeSearchNodeURL}", UrlConst.TREESEARCHNODE) //查询的controller地址
                            .replace("{viewColumn}", conf.getAttrColumn());
                }

            } else if (benModel.getClassType().equals("0")) {//如果本模型是列表
//				   script= script.replace("{viewName}", conf.getViewName())
//						      .replace("{viewColumn}", conf.getAttrColumn());
                if (model.getClassType().equals("1")) {//如果引用的树是父子树
                    //script= script.replace("{viewName}", conf.getViewName())
                    script = script.replace("{viewName}", model.getModelName())
                            .replace("{attrColumnTree}", conf.getTreeViewColumn()) //获取原模型列名
//							   .replace("{viewColumn}", benModel.getClassColumn()); 
                            .replace("{attrColumn}", conf.getTreeViewColumn())
                            .replace("{treeSearchNodeURL}", UrlConst.TREENODESURL) //查询的controller地址
                            .replace("{viewColumn}", conf.getTreeViewColumn());
                } else {
//					   script= script.replace("{viewName}", benModel.getTableName())
                    script = script.replace("{viewName}", benModel.getModelName())
                            .replace("{attrColumnTree}", conf.getAttrColumn()) //获取原模型列名
                            .replace("{attrColumn}", conf.getAttrColumn())
                            .replace("{treeSearchNodeURL}", UrlConst.TREESEARCHNODE) //查询的controller地址
                            .replace("{viewColumn}", benModel.getClassColumn());
                }
            }

            script = script.replace("{treeColumns}", columns)
                    .replace("{toorBar}", model.getTableName() + conf.getAttrColumn())
                    .replace("{treeSearchURL}", UrlConst.TREENODESURL) //查询的controller地址
                    //  .replace("{treeSearchNodeURL}", UrlConst.TREESEARCHNODE) //查询的controller地址
                    .replace("{modelIdTree}", conf.getModelId()) //获取原模型modelId

                    .replace("@R#", "\r\n") //替换换行
                    .replace("{surveyType}", model.getTableName()) //替换业务类型
                    .replace("{operType}", operType) //替换操作类型
                    .replace("{isParentType}", String.valueOf(conf.getIsParentType())) //选择列的id
                    .replace("{treeColId}", columnLowerCase) //选择列的id
                    .replace("{treeColName}", columnLowerCase + "Name"); //选择列的id
        } catch (Exception e) {
            e.printStackTrace();
        }
        return script;
    }

    /**
     * <p>TODO 树模板js页面，动态生成</p>
     * <p>@param model 模型
     * <p>@param fenModel 分类树左侧模型
     * <p>@param operType 操作类型
     * <p>@param configLst 属性设置列（All）
     *
     * @author : xiaoliu
     * @date :2017年5月2日上午10:17:55
     */
    public static String spellTreeJsReplace(Model model, Model fenModel, String operType, List<AttrConfig> configLst) {
        StringBuilder sb = new StringBuilder();
        List<AttrConfig> isPLst = new ArrayList<AttrConfig>();

        if (model.getClassType().equals("1")) {//如果是父子树
            if (configLst.size() > 0) {
                for (int i = 0; i < configLst.size(); i++) {
                    AttrConfig spleeCon = configLst.get(i);
                    if (1 == spleeCon.getIsParentType()) {//如果是父子树上下级关系
                        isPLst.add(spleeCon);
                    }
                }
            }
            if (isPLst.size() > 0) {
                for (int i = 0; i < isPLst.size(); i++) {
                    AttrConfig isDPLst = isPLst.get(i);
                    sb.append("   var zNodes" + isDPLst.getAttrColumn() + " = [{ id:0, " + isDPLst.getAttrColumn() + ":0, treeNodeName:'" + isDPLst.getViewName() + "', isParent:true}]; $RN$");
                    sb.append("     var setting = { $RN$");
                    sb.append("       async: { $RN$");
                    sb.append("       enable: true, $RN$");
                    sb.append("       type:'post', $RN$");
                    sb.append("       contentType:'application/x-www-form-urlencoded', $RN$");
                    sb.append("       url: basePath+'" + UrlConst.TREENODESURL + "',$RN$");
                    sb.append("       autoParam: ['id=" + isDPLst.getAttrColumn() + "'],$RN$");
                    sb.append("       otherParam: {'tableName' : '" + model.getTableName() + "','column':'" + isDPLst.getAttrColumn() + "'}$RN$");
                    sb.append("      },$RN$");
                    sb.append("      data: {$RN$");
                    sb.append("        key: {$RN$");
                    sb.append("        name: 'treeNodeName'$RN$");
                    sb.append("      },$RN$");
                    sb.append("      simpleData: {$RN$");
                    sb.append("         enable: true,$RN$");
                    sb.append("         idKey: 'id',$RN$");
                    sb.append("         pIdKey: '" + isDPLst.getAttrColumn() + "',$RN$");
                    sb.append("         rootPId: 0 $RN$ ");
                    sb.append("        } $RN$");
                    sb.append("      }, $RN$");
                    sb.append("     dataFilter: function(treeId, parentNode, responseData){ $RN$");
                    sb.append("     }, $RN$");
                    sb.append("     edit: { $RN$");
                    sb.append("       enable: false $RN$");
                    sb.append("     }, $RN$");
                    sb.append("     check: { $RN$");
                    sb.append("       enable: false $RN$");
                    sb.append("     }, $RN$");
                    sb.append("   view: {  $RN$");
                    sb.append("      selectedMulti: false, $RN$");
                    sb.append("      dblClickExpand: false  $RN$");
                    sb.append("    }, $RN$");
                    sb.append("   callback: {");
                    //多视图父子树，onclick事件更改，指定到具体视图
                    //sb.append("      onClick: zTreeOnClick $RN$");
                    sb.append("      onClick: zTreeOnClick" + isDPLst.getAttrColumn() + " $RN$");
                    sb.append("   } $RN$");
                    sb.append("  }; $RN$");
                    sb.append("   function initTree" + isDPLst.getAttrColumn() + "(data){ $RN$");
                    sb.append("      $.fn.zTree.init($('#" + model.getTableName() + "Tree" + isDPLst.getAttrColumn() + "'), setting, zNodes" + isDPLst.getAttrColumn() + "); $RN$");
                    sb.append("   } $RN$");
                    sb.append("   function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) { $RN$");
                    sb.append("   }; $RN$");
                    //多视图父子树，onclick事件更改，指定到具体视图
                    sb.append("  function zTreeOnClick" + isDPLst.getAttrColumn() + "(event, treeId, treeNode) { $RN$");
                    sb.append("      $('#" + JspUtil.spellColumn(model, Type.MAIN) + "').val(treeNode.id); $RN$");
                    sb.append("         var url = $('#menu_url').val(); $RN$");
                    sb.append("         var operColumn = '" + isDPLst.getAttrColumn() + "'; $RN$");
                    sb.append("         document.getElementById('" + model.getTableName() + "Frame').src=basePath+url" + "+'?" + JspUtil.spellColumn(model, Type.MAIN) + "='+treeNode.id+'&operColumn='+operColumn; $RN$");
                    //sb.append("         document.getElementById('"+model.getTableName()+"Frame').src=basePath+url"+"+'?"+JspUtil.spellColumn(model,Type.MAIN)+"='+treeNode.id; $RN$");
                    sb.append("  }; $RN$");
                    sb.append("  initTree" + isDPLst.getAttrColumn() + "(zNodes" + isDPLst.getAttrColumn() + "); $RN$");
                    //}

                }
            }
        } else if (model.getClassType().equals("2")) {//如果是分类树
            for (int i = 0; i < configLst.size(); i++) {
                AttrConfig fenConfig = configLst.get(i);
                if (fenConfig.getClassColumn().equals("1")) {
                    //System.out.println(fenConfig);
                    isPLst.add(fenConfig);
                }
            }
            //获取视图树的列名
            String column = isPLst.get(0).getTreeViewColumn();
            sb.append("   var zNodes" + model.getClassColumn() + " = [{ id:0, " + column + ":0, treeNodeName:'" + fenModel.getModelName() + "', isParent:true}]; $RN$");
            sb.append("     var setting = { $RN$");
            sb.append("       async: { $RN$");
            sb.append("       enable: true, $RN$");
            sb.append("       type:'post', $RN$");
            sb.append("       contentType:'application/x-www-form-urlencoded', $RN$");
            sb.append("       url: basePath+'" + UrlConst.TREENODESURL + "', $RN$");
            sb.append("       autoParam: ['id=" + column + "'], $RN$");
            sb.append("       otherParam: {'tableName' : '" + fenModel.getTableName() + "','column':'" + column + "'} $RN$");
            sb.append("      }, $RN$");
            sb.append("      data: { $RN$");
            sb.append("        key: { $RN$");
            sb.append("        name: 'treeNodeName' $RN$");
            sb.append("      }, $RN$");
            sb.append("      simpleData: { $RN$");
            sb.append("         enable: true, $RN$");
            sb.append("         idKey: 'id', $RN$");
            sb.append("         pIdKey: '" + column + "', $RN$");
            sb.append("         rootPId: 0 $RN$");
            sb.append("        } $RN$");
            sb.append("      }, $RN$");
            sb.append("     dataFilter: function(treeId, parentNode, responseData){ $RN$");
            sb.append("     }, $RN$");
            sb.append("     edit: { $RN$");
            sb.append("       enable: false $RN$");
            sb.append("     }, $RN$");
            sb.append("     check: { $RN$");
            sb.append("       enable: false $RN$");
            sb.append("     }, $RN$");
            sb.append("   view: {  $RN$");
            sb.append("      selectedMulti: false, $RN$");
            sb.append("      dblClickExpand: false $RN$");
            sb.append("    }, $RN$");
            sb.append("   callback: { $RN$");
            sb.append("      onClick: zTreeOnClick $RN$");
            sb.append("   } $RN$");
            sb.append("  }; $RN$");
            sb.append("   function initTree" + model.getClassColumn() + "(data){ $RN$");
            sb.append("      $.fn.zTree.init($('#" + fenModel.getTableName() + "Tree" + model.getClassColumn() + "'), setting, zNodes" + model.getClassColumn() + "); $RN$");
            sb.append("   } $RN$");
            sb.append("   function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) { $RN$");
            sb.append("   }; $RN$");
            sb.append("  function zTreeOnClick(event, treeId, treeNode) { $RN$");
            sb.append("      $('#" + JspUtil.spellColumn(model, Type.MAIN) + "').val(treeNode.id);");
            sb.append("         var url = $('#menu_url').val();");
            sb.append("         document.getElementById('" + model.getTableName() + "Frame').src=basePath+url" + "+'?" + JspUtil.spellColumn(model, Type.MAIN) + "='+treeNode.id;");
            sb.append("  }; $RN$");
            sb.append("  initTree" + model.getClassColumn() + "(zNodes" + model.getClassColumn() + "); $RN$");
        }
        return sb.toString().replace("$RN$", "\r\n");
    }

    public static String replacePopCaseStr(String readFilePath, Model model, String columns, String columnName, String caseCondition, AttrConfig confAttr, String operType) {
        String script = "";
        try {
            script = JspUtil.getTmpJspContent(readFilePath)  //根据路径获取自动生成的模板内容
                    .replace("{tableId}", model.getTableName() + "_" + columnName + "_pop") //表的id
                    .replace("{popColId}", columnName.toLowerCase()) //选择列的id
                    .replace("{popColumn}", columnName.toLowerCase() + "Name") //选择列的名
                    .replace("{popColumns}", columns) //表的id
                    .replace("{toorBar}", model.getTableName() + "_" + columnName + "_toorBar")
                    .replace("pageReqeust.filter={{cascad}}", caseCondition) //级联条件
                    .replace("{controlUrl}", UrlConst.POPLISTURL) //查询的controller地址
                    .replace("@R#", "\r\n") //替换换行
                    .replace("{modelId}", String.valueOf(model.getId())) //替换modelid
                    .replace("{surveyType}", model.getTableName()) //替换业务类型
                    .replace("{operColumn}", columnName)
                    .replace("{tableName}", model.getTableName())
                    .replace("{queryParams}", "queryParamsTab")
                    .replace("{idFieldRep}", "idField: \"id\",")
                    .replace("{popColRep}", "$(\"#" + columnName.toLowerCase() + "\").val(data[0].id); \r\n"
                            + "$(\"#" + columnName.toLowerCase() + "Name" + "\").val(" + columns + ");")
                    .replace("{clearPopJiLianData}", clearPopJiLianData(confAttr))//confAttr
                    .replace("{operType}", operType); //替换操作类型
        } catch (Exception e) {
            e.printStackTrace();
        }
        return script;
    }

    /**
     * <p>TODO 针对多视图拼写的动态替换参数条件的方法</p>
     * <p>@param model
     * <p>@param configLst
     * <p>@return </p>
     *
     * @author : xiaoliu
     * @date :2017年6月7日下午1:58:01
     */
    public static String spellViewColumn(Model model, String operType, List<AttrConfig> configLst) {
        StringBuilder sb = new StringBuilder();
        List<AttrConfig> isPLst = new ArrayList<AttrConfig>();
        if (configLst.size() > 0) {
            for (int i = 0; i < configLst.size(); i++) {
                AttrConfig spleeCon = configLst.get(i);
                if (1 == spleeCon.getIsParentType()) {//如果是父子树上下级关系
                    isPLst.add(spleeCon);
                }
            }
            if (isPLst.size() > 0) {
                sb.append("   if($('#operColumn').val()!=null){ \r\n");
                for (int i = 0; i < isPLst.size(); i++) {
                    AttrConfig cfg = isPLst.get(i);
                    if (cfg.getIsParentType() == 1) {//判断是否上下级关系
                        //获取当前操作属性的列名
                        String column = cfg.getAttrColumn();
                        //如果是上下级关系
                        sb.append("     if($('#operColumn').val()=='" + column + "'){ \r\n");
                        if (operType.equals("treeTabList") || operType.equals("treeTabZdList")) {
                            sb.append("        pageReqeust." + column + "=$(\"#ListPid\").val(); \r\n");
                        } else if (operType.equals("treeTabApply")) {
                            sb.append("        pageReqeust." + column + "=$(\"#ApplyPid\").val(); \r\n");
                        } else if (operType.equals("treeTabApprove")) {
                            sb.append("        pageReqeust." + column + "=$(\"#ShPid\").val(); \r\n");
                        } else if (operType.equals("treeTabUpdate")) {
                            sb.append("        pageReqeust." + column + "=$(\"#UpdatePid\").val(); \r\n");
                        } else if (operType.equals("treeTabWh")) {
                            sb.append("        pageReqeust." + column + "=$(\"#WhPid\").val(); \r\n");
                        }
                        sb.append("     } \r\n");
                    }
                }
                sb.append("  } \r\n");
            }
        }
        return sb.toString();

    }

    /**
     * <p>TODO 针对多视图拼写的动态替换参数条件的方法</p>
     * <p>@param model
     * <p>@param configLst
     * <p>@return </p>
     *
     * @author : xiaoliu
     * @date :2017年6月7日下午1:58:01
     */
    public static String spellfenColumn(Model model, String operType, List<AttrConfig> configLst) {
        StringBuilder sb = new StringBuilder();
        if (model.getClassType().equals("1")) {//这是父子树
            sb.append(spellViewColumn(model, operType, configLst));
        } else if (model.getClassType().equals("2")) {//这是分类树
            //sb.append(model.getClassColumn());
            String column = model.getClassColumn();
            if (operType.equals("treeTabList") || operType.equals("treeTabZdList")) {
                sb.append("     if($(\"#ListColumn\").val()!=null){ \r\n");
                sb.append("        pageReqeust." + column + "=$(\"#ListColumn\").val(); \r\n");
                sb.append("     } \r\n");
            } else if (operType.equals("treeTabApply")) {
                sb.append("     if($(\"#ApplyColumn\").val()!=null){ \r\n");
                sb.append("        pageReqeust." + column + "=$(\"#ApplyColumn\").val(); \r\n");
                sb.append("     } \r\n");
            } else if (operType.equals("treeTabApprove")) {
                sb.append("     if($(\"#ShColumn\").val()!=null){ \r\n");
                sb.append("        pageReqeust." + column + "=$(\"#ShColumn\").val(); \r\n");
                sb.append("     } \r\n");
            } else if (operType.equals("treeTabUpdate")) {
                sb.append("     if($(\"#UpdateColumn\").val()!=null){ \r\n");
                sb.append("        pageReqeust." + column + "=$(\"#UpdateColumn\").val(); \r\n");
                sb.append("     } \r\n");
            } else if (operType.equals("treeTabWh")) {
                sb.append("     if($(\"#WhColumn\").val()!=null){ \r\n");
                sb.append("        pageReqeust." + column + "=$(\"#WhColumn\").val(); \r\n");
                sb.append("     } \r\n");
            }
        }
        return sb.toString();

    }

    //添加一个弹出页面级联关系值改变的东西
    public static String clearPopJiLianData(AttrConfig confAttr) {
        StringBuilder sb = new StringBuilder();
        if (confAttr != null) {
            String column = confAttr.getAttrColumn();
            sb.append("$(\"#" + column.toLowerCase() + "\").val(\"\"); \r\n");
            sb.append("$(\"#" + column.toLowerCase() + "Name\").val(\"\"); \r\n");
        } else {
            sb.append("\r\n");
        }
        return sb.toString();

    }
}
